package com.songshan.controller;

import com.songshan.bean.Menu;
import com.songshan.dto.MenuNode;
import com.songshan.dto.Result;
import com.songshan.exception.BusinessException;
import com.songshan.service.MenuService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * @author feb
 * @date 2024/6/18
 * @Description: 系统菜单管理
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuManageController {

  @Autowired
  private MenuService menuService;

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 菜单管理页面
   */
  @RequestMapping("/manage.htmlx")
  public String menuManage() {
    return "menuManage";
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 菜单树
   */
  @PostMapping("/tree.do")
  @ResponseBody
  public List<MenuNode> menuTree(String mode) {
    return menuService.menuTree(mode);
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 菜单保存
   */
  @PostMapping("save.do")
  @ResponseBody
  public Result save(@Valid Menu menu, BindingResult errors) throws BusinessException{
    if (errors.hasErrors()) {
      return Result.failure(errors.getFieldErrors());
    }
    if (menu.getId() == null) {
      menuService.addMenu(menu);
    } else {
      menuService.modMenu(menu);
    }
    return Result.success();
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 菜单删除
   */
  @PostMapping("del.do")
  @ResponseBody
  public Result del(Integer id) throws BusinessException{
    menuService.delMenu(id);
    return Result.success();
  }
}
